package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Encoder;

import com.Szjcyh.Until.QueryJWModelData;
import com.limao.DBDataClass.CrowdUserAuthClass;
import com.limao.DBDataClass.CrowdUserShopDataClass;
import com.limao.DBDataClass.CrowdVRHomeClass;
import com.limao.DBDataClass.RMB_WithdrawDataClass;
import com.limao.DBDataClass.RedbagPrize;
import com.limao.DBDataClass.SysRedbagClass;
import com.limao.DBDataClass.SysStudyDataClass;
import com.limao.DBDataClass.UserRedBagDataClass;
import com.limao.DBDataClass.UserShopBuy5Sell5;
import com.limao.DBDataClass.VrGoodsDataClass;
import com.limao.DBDataClass.VrGoodsNumClass;
import com.limao.DBProvider.CrowdPrestigeOrderProvider;
import com.limao.DBProvider.CrowdUserAuthProvider;
import com.limao.DBProvider.CrowdUserShopProvider;
import com.limao.DBProvider.UserRedbagProvider;
import com.limao.DBProvider.VrGoodsUserNumProvider;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Model.RecvData;
import com.limao.Tool.CacheClass;
import com.limao.Utils.OutPrintClass;

public class UserInfo {
	
	
	
	private static String htmlHead="<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/>"+
    "<meta content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\" name=\"viewport\">"+
    "<title></title><style>html,body,img,video{width:99%;max-width:100%;overflow:hidden;} </style></head><body><div style=\"margin:0 auto;width: 95%;\">";
    
	private static String htmlEnd="</div></body></html>";
	
	//&新鲜宝  取某人的注册信息
	public static Object getLkAuthInfo(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
		//参数检查3（数据合法性检查）
		int userid=CacheClass.getIntValue(DataObj.userid);

		CrowdUserAuthClass authTmp=CrowdUserAuthCache.getInstance().getUser(userid);

		if(authTmp==null)
		{
			throw new LzPluginException("出错,没有该用户");
   		}

		PublicData data = new PublicData();
		data.state = "1";
		data.info = "获取用信息成功";
		data.m = m + "";
		data.obj=authTmp;
		
		return data;
		
	}
	
	
	//&新鲜宝  修改本人人的呢称
	public static Object SetAuthName(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if(CacheClass.isEmpty(DataObj.name))
   		{
			throw new LzPluginException("出错,参数出错");
   		}
		//取现在的呢称
		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass authTmp=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(DataObj.userid));
		if(CacheClass.myEqualsIgnoreCase(DataObj.name, authTmp.username))
		{
			throw new LzPluginException("出错,新呢称与原来呢称一致");
		}
		
		authTmp=CrowdUserAuthCache.getInstance().UPDATA_User_auth_name(userid,DataObj.name);
		if(authTmp==null )
		{
			throw new LzPluginException("修改用户呢称失败");
   		}

		return OutPrintClass.resultObj(m,"修改用户呢称成功");

	}	
	
	//&新鲜宝   修改用户头像信息
	public static Object SetAuth_AvatarInfo(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
   		CrowdUserAuthClass tmpAuth = null;

		//写人物头像信息
		int userid=CacheClass.getIntValue(DataObj.userid);
		tmpAuth = CrowdUserAuthCache.getInstance().UPDATA_User_auth_Avatar(userid,DataObj.avatar);

 		if(tmpAuth==null)
 		{
			throw new LzPluginException("修改用户头像信息失败");
 		}

		//成功
 		return OutPrintClass.resultObj(m,"修改用户头像信息成功");

	}
	
	
	//&新鲜宝  修改用户基本信息
	public static Object SetAuth_BaseInfo(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
   		CrowdUserAuthClass tmpAuth = null;

		int userid=CacheClass.getIntValue(DataObj.userid);
		tmpAuth = CrowdUserAuthCache.getInstance().UPDATA_User_auth_Base(userid,DataObj.sign);

 		if(tmpAuth==null)
 		{
			throw new LzPluginException("修改用户基本信息失败");
 		}

		//成功
 		return OutPrintClass.resultObj(m,"修改用户基本信息成功");

	}
	
	
	
	
	//&新鲜宝  修改本人的提现绑定
	public static Object SetAuth_BankInfo(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
   		CrowdUserAuthClass tmpAuth = null;

		int userid=CacheClass.getIntValue(DataObj.userid);
   		if(CacheClass.isEmpty(DataObj.cardtype)){
   			DataObj.cardtype = "0";
   		}
   		int cardtype=CacheClass.getIntValue(DataObj.cardtype);
   		if(cardtype==0) //银行卡
   		{
   			if(CacheClass.isEmpty(DataObj.cardno) || CacheClass.isEmpty(DataObj.cardname) || CacheClass.isEmpty(DataObj.cardbank))
   			{
   				throw new LzPluginException("参数不能为空");
   			}
   		}
   		else
   		{
   			if(CacheClass.isEmpty(DataObj.cardno))
   			{
   				throw new LzPluginException("参数不能为空");
   			}
   		}
   		
		//更新提现绑定

		tmpAuth = CrowdUserAuthCache.getInstance().UPDATA_User_auth_Bank(userid,cardtype,DataObj.cardno,DataObj.cardname,DataObj.cardbank,DataObj.cardarea);

 		if(tmpAuth==null)
 		{
			throw new LzPluginException("修改提现绑定信息失败");
 		}

		//成功
 		return OutPrintClass.resultObj(m,"修改提现绑定信息成功");

 		
	}
		

	
	//&新鲜宝 提现
	public static Object RMB_Withdraw(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
		if(CacheClass.isEmpty(DataObj.password) || CacheClass.isEmpty(DataObj.rmb))
   		{
			throw new LzPluginException("参数不正确");
   		}
		//参数检查3（数据合法性检查）
		int userid=CacheClass.getIntValue(DataObj.userid);
		if(CacheClass.isEmpty(DataObj.cardtype)){
   			DataObj.cardtype = "0";
   		}
   		int cardtype=CacheClass.getIntValue(DataObj.cardtype);
   		if(cardtype==0) //银行卡
   		{
   			if(CacheClass.isEmpty(DataObj.cardno) || CacheClass.isEmpty(DataObj.cardname) )
   			{
   				throw new LzPluginException("参数不能为空");
   			}
   			if(DataObj.cardarea.contains("交通银行") || DataObj.cardarea.contains("交行") || DataObj.cardbank.contains("交通银行") || DataObj.cardbank.contains("交行"))
   			{
   				throw new LzPluginException("提现银行，不能为交通银行！");
   			}
/*   			
   			if(!CacheClass.isBank(DataObj.cardbank))
   			{
   				throw new LzPluginException("开户行格式须为： XXXX银行XX支行(或营业部或分理处)");
   			}
   			
   			DataObj.cardname,DataObj.cardbank
 */
   			
   		}
   		else
   		{
   			if(CacheClass.isEmpty(DataObj.cardno) || CacheClass.isEmpty(DataObj.cardname))
   			{
   				throw new LzPluginException("参数不能为空");
   			}
   		}

	    float rmb=CacheClass.getFloatValue(DataObj.rmb);
		if(userid<0 ) 
		{
			throw new LzPluginException("用户没有登陆");
		}
		if(rmb<=0) 
		{
			throw new LzPluginException("金额不正确");
		}		
		
		//密码是否正确
		if(!CrowdUserAuthCache.getInstance().passIsValid(userid,DataObj.password.toUpperCase()))
		{
			throw new LzPluginException("密码不正确");
		}
		
		//用户基本信息
		CrowdUserAuthClass auth=CrowdUserAuthCache.getInstance().getUser(userid);
		if(auth==null)		
		{
			throw new LzPluginException("用户不存在");
		}
		
		
		//一个月
		
		
		//一周内，最多只提现2次
		if(!RmbwithdrawCacheManager.getInstance().load_WithdrawByIdNum(userid)  )
		{
			int withdrawDay=CacheClass.getIntValue(BaseDataCacheManager.getInstance().Load_Param("withdrawDay"));
			int withdrawNum=CacheClass.getIntValue(BaseDataCacheManager.getInstance().Load_Param("withdrawNum"));
			int withdrawDayShow=CacheClass.getIntValue(BaseDataCacheManager.getInstance().Load_Param("withdrawDayShow"));
			throw new LzPluginException("您的提现太过频繁，连续"+withdrawDayShow+"工作日内提现不能超过"+withdrawNum+"次，请过几天再试");
		}	
		
		
		//是否禁用
		if(CacheClass.getIntValue(auth.isdisabled)==1  )
		{
			throw new LzPluginException("您的被禁用");
		}		
		//是否积分冻结
		if(CacheClass.getIntValue(auth.rmbstate)==1  )
		{
			throw new LzPluginException("您的资产被冻结");
		}	
			
		float rmb0=CacheClass.getFloatValue(auth.nowrmb );
		if(rmb>rmb0)
		{
			throw new LzPluginException("可提现金额不足");
		}
/*		
		float rmb0=CacheClass.getFloatValue(auth.nowrmb )-CacheClass.getFloatValue(auth.principlelrmb);
		if(rmb>rmb0)
		{
			throw new LzPluginException("可提现金额不足");
		}
*/
	   	//更新提现绑定
   		CrowdUserAuthClass tmpAuth = CrowdUserAuthCache.getInstance().UPDATA_User_auth_Bank(userid,cardtype,DataObj.cardno,DataObj.cardname,DataObj.cardbank,DataObj.cardarea);
 		if(tmpAuth==null)
 		{
			throw new LzPluginException("修改提现绑定信息失败");
 		}
 		
		if(!RmbwithdrawCacheManager.getInstance().Add_WithdrawOrder(userid,rmb,CacheClass.getFloatValue(auth.nowrmb )))
		{
			throw new LzPluginException("操作失败");
		}


		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.info = "申请成功,会在三个工作日内处理";
		data.m = m + "";
		
		return data;
		
	}
	
	
	//&新鲜宝   取用户提现列表（所有）
	public static Object GET_RMB_WithdrawLIST(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{

		if (CacheClass.isEmpty(DataObj.curpage))
			DataObj.curpage = "1";

		// 参数检查（值范围、正确性检查）
		int curpage = CacheClass.getIntValue(DataObj.curpage);

		int userid = CacheClass.getIntValue(DataObj.userid);
		
		PublicData data=RmbwithdrawCacheManager.getInstance().load_WithdrawOrderList(userid, curpage, 0, 0);

		data.state = "1";
		data.m = m+"";
		data.info = "获取提现列表成功";

		return data;
		
	}	
	
	//&新鲜宝  查询用户提现单详细情况 by id
	public static Object GET_RMB_WithdrawDETAIL(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查
		if (CacheClass.isEmpty(DataObj.id)) {
			throw new LzPluginException("内容为空或者参数不正确");
		}
		// 参数检查（值范围、正确性检查）
		int withdrawid = CacheClass.getIntValue(DataObj.id);
		int userid = CacheClass.getIntValue(DataObj.userid);

		RMB_WithdrawDataClass tmp = RmbwithdrawCacheManager.getInstance().load_WithdrawById(withdrawid);
		if (tmp == null) {
			throw new LzPluginException("数据库操作出错");
		}

		PublicData data = new PublicData();
		data.obj = tmp;
		data.state = "1";
		data.m = m+"";
		data.info = "获取详情成功";
		
		return data;
		
	}
	
	
	//***************************************************************************

	
	//&新鲜宝     取用户收益统计列表
	public static Object load_RMB_List(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
		if(CacheClass.isEmpty(DataObj.curpage))
   		{
			DataObj.curpage ="1";
   		}		

		PublicData data=user_rmbCacheManager.getInstance().get_Sys_RMB_List(CacheClass.getIntValue(DataObj.userid),CacheClass.getIntValue(DataObj.curpage));

		if(data==null)
		{
			throw new LzPluginException("数据库操作出错");
		}
		// 生成result包给用户	
		data.state = "1";
		data.m = m+"";
		data.info = "用户收益列表成功";

		return data;
		
		
	}
	

	
	//&新鲜宝     取用户额度记录列表
	public static Object load_RMB_List2(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
		if(CacheClass.isEmpty(DataObj.curpage))
   		{
			DataObj.curpage ="1";
   		}		

		PublicData data=user_rmbCacheManager.getInstance().get_Sys_RMB_List4(CacheClass.getIntValue(DataObj.userid),CacheClass.getIntValue(DataObj.curpage));

		if(data==null)
		{
			throw new LzPluginException("数据库操作出错");
		}
		// 生成result包给用户	
		data.state = "1";
		data.m = m+"";
		data.info = "用户现货瓶数记录列表成功";

		return data;
		
		
	}
	
	
	
	//新鲜宝    取账单列表
	public static Object load_RMB_List3(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		
		if(CacheClass.isEmpty(DataObj.curpage))
   		{
			DataObj.curpage ="1";
   		}		
		

		PublicData data=user_rmbCacheManager.getInstance().get_Sys_RMB_List3(CacheClass.getIntValue(DataObj.userid),CacheClass.getIntValue(DataObj.curpage));

		if(data==null)
		{
			throw new LzPluginException("数据库操作出错");
		}
		// 生成result包给用户
		
	
		data.state = "1";
		data.m = m+"";
		data.info = "获取账单列表成功";

		return data;
		
		
	}
	
	//&新鲜宝 取用户团队列表
	public static Object load_MANS_LIST(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{

		if(CacheClass.isEmpty(DataObj.curpage))
   		{
			DataObj.curpage ="1";
   		}		
		if(CacheClass.isEmpty(DataObj.keyword))
   		{
			DataObj.keyword ="";
   		}
		
		PublicData data=user_rmbCacheManager.getInstance().get_Sys_MANS_List(CacheClass.getIntValue(DataObj.userid),DataObj.keyword,CacheClass.getIntValue(DataObj.curpage));

		if(data==null)
		{
			throw new LzPluginException("数据库操作出错");
		}
		// 生成result包给用户		
		data.state = "1";
		data.m = m+"";
		data.info = "获销售团队列表成功";

		return data;
		
		
	}	
	

	//&新鲜宝	市场交易帐户开启
	public static Object GET_VRHomeOpenUser(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if (CacheClass.isEmpty(DataObj.userid)) {
			throw new LzPluginException("没有登录不能操作");
		}
		
		int userid = CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass tmp2=CrowdUserAuthCache.getInstance().getUser(userid);
		if(CacheClass.getIntValue(tmp2.isvruser)!=0)
		{
			throw new LzPluginException("市场交易帐户您已开启过了!");
		}
		CrowdUserAuthClass tmp=CrowdUserAuthCache.getInstance().UPDATA_User_auth_Isvruser(userid);
		
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "市场交易帐户开启成功";
		goods.obj=tmp;
		
		return goods;
		
	}
	
	
	//&新鲜宝	转入资金取信息
	public static Object GET_VRPUTINRMBGetUser(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if (CacheClass.isEmpty(DataObj.userid)) {
			throw new LzPluginException("没有登录不能操作");
		}
		int userid = CacheClass.getIntValue(DataObj.userid);
		
		CrowdVRHomeClass tmp=CrowdVRHomeCache.getInstance().Load_CrowdVR();
		CrowdUserAuthClass tmp2=CrowdUserAuthCache.getInstance().getUser(userid);

		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "转入资金--取信息成功";

		goods.nowrmb =tmp2.nowrmb ;
		goods.principlelrmb=tmp2.principlelrmb;
		
		goods.vrid=tmp.vrid;
		goods.iscommon=tmp.iscommon;
		goods.commonrate=tmp.commonrate;
		goods.marate=tmp.marate;
		goods.type=tmp.type;

		
		return goods;
		
	}
	

	//&新鲜宝	排名
	public static Object getRankInfo(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
	
		if(CacheClass.isEmpty(DataObj.curpage))
   		{
			DataObj.curpage ="1";
   		}		
		if(CacheClass.isEmpty(DataObj.type))
   		{
			//0:收益 1:活跃排行
			DataObj.type ="1";
   		}		
		
		PublicData data=user_rmbCacheManager.getInstance().getRankInfo(CacheClass.getIntValue(DataObj.type),CacheClass.getIntValue(DataObj.curpage));

		if(data==null)
		{
			throw new LzPluginException("数据库操作出错");
		}
		// 生成result包给用户		
		data.state = "1";
		data.m = m+"";
		data.info = "获取排行榜成功";

		return data;
	}
	
	//&新鲜宝	新手课堂
	public static Object Study_List0(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查
		ArrayList<SysStudyDataClass> objList=Sys_StudyCacheManager.getInstance().Load_SySMatter_All();
		if(objList!=null)
		{
	   		for(SysStudyDataClass obj:objList)
			{
				obj.studycontent=null;
			}
		}
		ArrayList<Object> list = new ArrayList<Object>();
		if(objList!=null)
		{
			list.addAll(objList);
		}
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "取新手课堂列表成功";
		goods.list=list;
		
		return goods;
	}	
	
	//&新鲜宝	公司活动
	public static Object Study_List1(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查
		ArrayList<SysStudyDataClass> objList=Sys_StudyCacheManager.getInstance().Load_SySNewRule_All();
		if(objList!=null)
		{
	   		for(SysStudyDataClass obj:objList)
			{
				obj.studycontent=null;
			}
		}
		ArrayList<Object> list = new ArrayList<Object>();
		if(objList!=null)
		{
			list.addAll(objList);
		}
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "取公司活动列表成功";
		goods.list=list;
		
		return goods;
	}	
	
	public static Object Load_SySStudy_ById(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if (CacheClass.isEmpty(DataObj.studyid)) {
			throw new LzPluginException("参数不正确");
		}
		// 参数检查
		SysStudyDataClass tmp=Sys_StudyCacheManager.getInstance().Load_SySNewRule_ById(DataObj.studyid);
		if(tmp==null)
		{
			throw new LzPluginException("内容不存在");
		}
		
		
		String result=htmlHead+tmp.studycontent+htmlEnd;
		
		byte[] b = null;  
        try 
        {  
        	b = result.getBytes("utf-8");
        	if (b != null) {  
        		result = new BASE64Encoder().encode(b);
            }      
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
		
        tmp.studycontent=result;
        
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "取详情成功";
		goods.obj=tmp;
		result=null;
		return goods;
	}
	
	
	//开通交易帐户
	public static Object OPEN_USERINFO_TRADE_USER(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if (CacheClass.isEmpty(DataObj.userid)) {
			throw new LzPluginException("没有登录不能操作");
		}
		//检查是否已开通过了
		
		
		
		// 参数检查
		CrowdUserAuthProvider.getInstance().openAuthForTradeUser(DataObj.userid);
		
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "开通交易帐户成功";

		
		return goods;
	}
	
	public static Object SET_MT_TOKEN(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if(CacheClass.isEmpty(DataObj.userid) || CacheClass.isEmpty(DataObj.mt_token) ) //uid:mt_token
   		{
			throw new LzPluginException("出错,参数出错");
   		}
		//取现在的呢称
		int userid=CacheClass.getIntValue(DataObj.userid);
		String mt_token=DataObj.mt_token.trim();
		if(CrowdUserAuthCache.getInstance().UPDATA_User_mt_token(userid,mt_token)==null )
		{
			throw new LzPluginException("更新token失败");
   		}

		return OutPrintClass.resultObj(m,"更新token成功");

	}
	
	
	//取用户宝箱列表	
	public static Object GET_LIST_OPERATION_REDBAG(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		int userid=CacheClass.getIntValue(DataObj.userid);
		
		List<UserRedBagDataClass> list = UserRedbagProvider.getInstance().Load_UserRedbagList(userid);
		
		List<Object> list2= new ArrayList<Object>();
		if(list!=null && list.size()>0)
			list2.addAll(list);
		
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "取宝箱列表成功";
		data.list=list2;
		
		return data;
		
	}
	
	
	//用户打开宝箱
	public static Object OPEN_OPERATION_REDBAG(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		if(CacheClass.isEmpty(DataObj.userredbagid))
		{
			throw new LzPluginException("出错,参数出错");
		}
		int userid=CacheClass.getIntValue(DataObj.userid);
		int userredbagid=CacheClass.getIntValue(DataObj.userredbagid);
		
		UserRedBagDataClass obj=UserRedbagProvider.getInstance().Load_UserRedbagByuseridRedbagid(userredbagid,userid);
		if(obj==null)
		{
			throw new LzPluginException("宝箱不存在");
		}
		if(CacheClass.getIntValue(obj.state)!=0) //0没打开过，1打开过 2已过期 3订单取消而失效
		{
			if(CacheClass.getIntValue(obj.state)==1)
			{
				throw new LzPluginException("宝箱打开过了。");
			}
			if(CacheClass.getIntValue(obj.state)==2)
			{
				throw new LzPluginException("宝箱已过期。");
			}
			if(CacheClass.getIntValue(obj.state)==3)
			{
				throw new LzPluginException("宝箱因订单取消而失效");
			}
		}
		
		//取出对应的系统红包设置项
		SysRedbagClass tmp=SysRedbagCache.getInstance().Load_SysRedbagByid(CacheClass.getIntValue(obj.redbagid));
		if(tmp==null)
		{
			throw new LzPluginException("宝箱参数不存在");
		}
		//计算出获得的奖品项
	
		//计算总权重
		float sumWeight=0;
		for(RedbagPrize p:tmp.prizelist)
		{
			sumWeight=sumWeight+CacheClass.getFloatValue(p.rate);
		}
		
		float ran=CacheClass.getRandomNum(0,sumWeight);
		
		float d1=0;
		RedbagPrize rp=null;
		for(RedbagPrize p:tmp.prizelist)
		{
			d1 = d1+CacheClass.getFloatValue(p.rate);
			if(ran<d1)
			{
			   rp=p;
			   break;
			}
       }
		System.out.println(" 随机数="+ran+"，中奖序号="+rp.sellOne);
		
	   if(rp==null)
	   {
		   throw new LzPluginException("机率计算出错");
	   }
	   //更新用户红包数据
	   UserRedBagDataClass userRB= UserRedbagProvider.getInstance().Update_UserRedbag(userredbagid,rp);
	   if(userRB==null)
	   {
		   throw new LzPluginException("打开宝箱失败");
	   }
	   

	   //返回数据
		List<UserRedBagDataClass> list = UserRedbagProvider.getInstance().Load_UserRedbagList(userid);
		
		List<Object> list2= new ArrayList<Object>();
		if(list!=null && list.size()>0)
			list2.addAll(list);
		
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "打开宝箱成功";
		data.obj=tmp;
		data.sellOne=rp.sellOne;
		
		return data;
		
	}
	
	//用户发布寄售  	
	public static Object SET_USERSHOP_SELLBUY_OPERATION_SELL(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		if(CacheClass.isEmpty(DataObj.vrgoodsid) || CacheClass.isEmpty(DataObj.num) || CacheClass.isEmpty(DataObj.price))
		{
			throw new LzPluginException("参数不正确。");
		}
		if(CacheClass.isEmpty(DataObj.type))
		{
			DataObj.type="0"; 
		}
		int userid=CacheClass.getIntValue(DataObj.userid);
		int vrgoodsid=CacheClass.getIntValue(DataObj.vrgoodsid);
		
		float num=CacheClass.getFloatValue(DataObj.num);
		float price=CacheClass.getFloatValue(DataObj.price);
		float rmb=price*num;
		int type=CacheClass.getIntValue(DataObj.type);
		
		if(num<=0 || price<=0)
		{
			throw new LzPluginException("参数不正确。");
		}
		
		//发布寄售
		if(CrowdUserShopProvider.getInstance().Create_UserShopGoods(userid,vrgoodsid,num,rmb,price,type))
		{
			throw new LzPluginException("发布寄售失败。");
		}
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "寄售发布成功";
		
		return data;
		
	}
	
	//用户查看寄售列表
	public static Object GET_USERSHOP_SELLBUY_OPERATION_LIST(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		if(CacheClass.isEmpty(DataObj.vrgoodsid) || CacheClass.isEmpty(DataObj.type))
		{
			throw new LzPluginException("参数不正确。");
		}
		
		if (CacheClass.isEmpty(DataObj.curpage))
			DataObj.curpage = "1";
		
		PublicData goods = CrowdUserShopProvider.getInstance().getUserShopGoodsList(CacheClass.getIntValue(DataObj.vrgoodsid),CacheClass.getIntValue(DataObj.type),CacheClass.getIntValue(DataObj.curpage),DataObj.keyword.trim());
		if (goods == null) {
			throw new LzPluginException("获取寄售列表失败");
		}

		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "获取寄售列表成功";
		
		return goods;
		
	}
	
	
	//用户购买寄售  	
	public static Object GET_USERSHOP_SELLBUY_OPERATION_BUY(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		if(CacheClass.isEmpty(DataObj.sellid) )
		{
			throw new LzPluginException("参数不正确。");
		}
		int userid=CacheClass.getIntValue(DataObj.userid);
		long sellid=CacheClass.getLongValue(DataObj.sellid);
		
		CrowdUserShopDataClass tmp = CrowdUserShopProvider.getInstance().loadUserShopGoodsBySellid(sellid);
		if(tmp==null)
		{
			throw new LzPluginException("该寄售不存。");
		}
		int type=CacheClass.getIntValue(tmp.type);
		int state=CacheClass.getIntValue(tmp.state);
		if(state!=1)
		{
			if(state==0)
			{
				throw new LzPluginException("出错，寄售还未上架。");
			}
			else if(state==2)
			{
				throw new LzPluginException("出错，寄售已出成交了。");
			}
			else if(state==3)
			{
				throw new LzPluginException("出错，寄售已撒消了 。");
			}
		}		
		float num=0;
		if(type==0) //该寄售是出售 ，那么本操作就为采购
		{
			num=CacheClass.getFloatValue(tmp.num);
		}
		else //该寄售是采购 ，那么本操作就为出售
		{
			if(CacheClass.isEmpty(DataObj.num) )
			{
				throw new LzPluginException("参数不正确。");
			}
			num=CacheClass.getFloatValue(DataObj.num);
			if(num<=0)
			{
				throw new LzPluginException("参数不正确。");
			}
			if(num>(CacheClass.getFloatValue(tmp.num)-CacheClass.getFloatValue(tmp.numed)))
			{
				throw new LzPluginException("数量太多了");
			}
			
			//15天前的瓶数
			VrGoodsNumClass userVrgoodsNum=VrGoodsUserNumProvider.getInstance().getUserVrGoodsNum(CacheClass.getIntValue(tmp.vrgoodsid), userid);
			if(userVrgoodsNum==null || num>CacheClass.getFloatValue(userVrgoodsNum.num))
			{
				throw new LzPluginException("你的现货瓶数不足。");
			}
			if(num>CacheClass.getFloatValue(userVrgoodsNum.num15days))
			{
				int inFoundvailDay=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("inFoundvailDay").trim());
				throw new LzPluginException("您可出售的瓶数（"+inFoundvailDay+"天前）不足");
			}

		}
		
		//购买（出售）寄售
		if(CrowdUserShopProvider.getInstance().Buy_UserShopGoods(userid,tmp,sellid,type,num))
		{
			if(type==0)
				throw new LzPluginException("购买寄售失败。");
			else
				throw new LzPluginException("出售失败。");
				
		}
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "操作成功";
		
		return data;
		
	}
	
	
	//取寄售卖买前5
	public static Object GET_USERSHOP_SELLBUY_OPERATION_RANK5(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if(CacheClass.isEmpty(DataObj.vrgoodsid))
		{
			throw new LzPluginException("参数不正确。");
		}
		
		int vrgoodsid=CacheClass.getIntValue(DataObj.vrgoodsid);
		
		// 买前5
		List<UserShopBuy5Sell5>  list=CrowdUserShopProvider.getInstance().getUserShopGoodsListForBuy5(vrgoodsid);
		
		// 卖前5
		List<UserShopBuy5Sell5>  list2=CrowdUserShopProvider.getInstance().getUserShopGoodsListForSell5(vrgoodsid);

		List<Object> tolist= null;
		List<Object> tolist2= null;
		if(list.size()>0)
		{
			tolist= new ArrayList<Object>();
			tolist.addAll(list);
		}
			
		if(list2.size()>0)
		{
			tolist2= new ArrayList<Object>();
			tolist2.addAll(list2);
		}
			
		
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "获取买卖前五档成功";
		data.list=tolist;
		data.list2=tolist2;
		
		return data;
		
	}
	
	
	//取寄售近30天日成交均价
	public static Object GET_USERSHOP_SELLBUY_OPERATION_30DAYS_PRICE(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if(CacheClass.isEmpty(DataObj.vrgoodsid))
		{
			throw new LzPluginException("参数不正确。");
		}
		
		int vrgoodsid=CacheClass.getIntValue(DataObj.vrgoodsid);
		
		// 买前5
		List<UserShopBuy5Sell5>  list=CrowdUserShopProvider.getInstance().getUserShopGoodsListFor30DaysPrice(vrgoodsid);
		
		List<Object> tolist= null;
		if(list.size()>0)
		{
			tolist= new ArrayList<Object>();
			tolist.addAll(list);
		}
			
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "获取近30天日成交均价成功";
		data.list=tolist;
		
		return data;
		
	}
	
	
	//酒交所开户
	public static Object USER_TRADESYS_OPERATION_REG(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if(CacheClass.isEmpty(DataObj.Name) || CacheClass.isEmpty(DataObj.CertificateType)
				 || CacheClass.isEmpty(DataObj.CertificateNo) || CacheClass.isEmpty(DataObj.Mobile)
				 || CacheClass.isEmpty(DataObj.Bank) || CacheClass.isEmpty(DataObj.BankAccount)
				 || CacheClass.isEmpty(DataObj.Address) || CacheClass.isEmpty(DataObj.Email)
				 || CacheClass.isEmpty(DataObj.Picture))
		{
			throw new LzPluginException("参数不正确。");
		}
		if(!CacheClass.isMobileNO(DataObj.Mobile))
		{
			throw new LzPluginException("出错,手机号格式不正确");
		}
		if(!CacheClass.isEMailNO(DataObj.Email))
		{
			throw new LzPluginException("出错,邮箱格式不正确");
		}
		if(!CacheClass.checkBankCard(DataObj.BankAccount))
		{
			throw new LzPluginException("出错,请输入正确的卡号");
		}		
		if(CacheClass.getIntValue(DataObj.CertificateType)==1 && !CacheClass.isIdNum(DataObj.CertificateNo))
		{
			throw new LzPluginException("出错,请输入正确的身份证号");
		}			
		
		QueryJWModelData querData=new QueryJWModelData();
		
		querData.Name = DataObj.Name;//姓名
		querData.Type = "3";//交易商类型 1：企业2：代理3：个人
		querData.ContactMan = DataObj.Name;//联系人
		querData.CertificateType = DataObj.CertificateType;//证件类型 1：居民身份证2：士官证3：学生证4：驾驶证5：护照6：港澳通行证
		querData.CertificateNo = DataObj.CertificateNo;//证件号码
		querData.Mobile = DataObj.Mobile;//联系人手机号
		querData.Bank = DataObj.Bank;//开户银行12：中行13：建行15：招行18：平安005：农行
		querData.BankAccount = DataObj.BankAccount;//银行帐号
		querData.Address = DataObj.Address;//地址
		querData.Email = DataObj.Email;//Email
		querData.BrokerId = "Z107";//开户机构 z03    居间商可以不填
		querData.BrokerAgeId = "";//居间商
		querData.Picture = DataObj.Picture;//身份证正面照	
				
		String userid=DataObj.userid;
		CrowdUserAuthProvider.getInstance().openAuthForTradeUser(userid,querData);
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "【开户申请】提交成功，请等待审核";
		
		return data;
		
	}	
	
	
	//酒交所开户
	public static Object USER_TRADESYS_OPERATION_REGING(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass auth=CrowdUserAuthCache.getInstance().getUser(userid);
		if(auth==null)		
		{
			throw new LzPluginException("用户不存在");
		}
		long applyid =CacheClass.getLongValue(auth.applyid);
		int applystateint =CacheClass.getIntValue(auth.applystate);
		if(applyid==0) //此用户已户
		{
			throw new LzPluginException( "您还未开户，不能进入。");
		}
		else
		{
			switch(applystateint)
			{
			case 3:
				break;
			case 0:  //审核中 
			case 2:
				//去查进度
				applystateint=CrowdUserAuthProvider.getInstance().QueryUserTradeApplyState(userid,auth.applyid,applystateint);
				if(applystateint!=1) //不等于跳出然后返，否则继续向下运行，去登陆
				{
					break;
				}
			case 1:
				//去登陆
				String sessionid=CrowdUserAuthProvider.getInstance().LoginAuthForTrade(userid+"",false,"");
				// 生成result包给用户
				PublicData data = new PublicData();
				data.state = "1";
				data.m = m + "";
				data.info = "交易系统 登陆成功";
				data.applystate=applystateint+"";
				data.mt_token=sessionid;
				return data;
			}
		}
		
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "查询成功";
		data.applystate=applystateint+"";
		return data;
		
	}	
	
	//酒交所转入瓶数
	public static Object USER_TRADESYS_OPERATION_IN_HOLD(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		if(CacheClass.isEmpty(DataObj.vrgoodsid) || CacheClass.isEmpty(DataObj.num) )
		{
			throw new LzPluginException( "参数不正确。");
		}
		int vrgoodsid=CacheClass.getIntValue(DataObj.vrgoodsid);
		if(!CacheClass.isIntegerNumber2(DataObj.num))
		{
			throw new LzPluginException("参数不正确。");
		}
		int num=CacheClass.getIntValue(DataObj.num);
		if(num<=0)
		{
			throw new LzPluginException("参数不正确。");
		}

		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass auth=CrowdUserAuthCache.getInstance().getUser(userid);
		if(auth==null)		
		{
			throw new LzPluginException("用户不存在");
		}
		int applystateint =CacheClass.getIntValue(auth.applystate);
		if(applystateint!=1) //此用户已户
		{
			throw new LzPluginException( "您的开户还未完成，不能转入。");
		}

		if(num>CacheClass.getFloatValue(auth.manowtotalrmb))
		{
			throw new LzPluginException( "您的数量不足，不能转入。");
		}
		
		//转入
		if(CrowdUserShopProvider.getInstance().Create_IN_Trade(userid,vrgoodsid,num))
		{
			throw new LzPluginException("【转入申请】提交失败。");
		}
		
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "【转入申请】提交成功，请在第二日开市后查询转入结果。";
		data.applystate=applystateint+"";
		return data;
		
	}	
	
	
	//现货列表
	public static Object USER_TRADESYS_OPERATION_VRGOODS_List(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		int userid=CacheClass.getIntValue(DataObj.userid);
		
		List<VrGoodsDataClass> list= VrGoodsCache.getInstance().Load_All_VrGoodsList();
		List<Object> list2= null;
		if(list.size()>0)
		{
			//取用户现货瓶数
			list=VrGoodsUserNumProvider.getInstance().getUserVrGoodsNum(list,userid);
			
			list2= new ArrayList<Object>();
			list2.addAll(list);
		}
		
		PublicData goods=new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "获取现货列表成功";
		goods.list=list2;
		
		return goods;
		
	}
	
	//现货 　申请发货	
	public static Object USER_TRADESYS_OPERATION_VRORDER(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		int userid=CacheClass.getIntValue(DataObj.userid);
		
		if(CacheClass.isEmpty(DataObj.vrgoodsid) || CacheClass.isEmpty(DataObj.num) || 
				!CacheClass.isIntegerNumber2(DataObj.num) || CacheClass.isEmpty(DataObj.address))
		{
			throw new LzPluginException("参数不正确。");
		}
		
		int num=CacheClass.getIntValue(DataObj.num);
		
		//瓶数是否足够
		VrGoodsNumClass userVrgoodsNum=VrGoodsUserNumProvider.getInstance().getUserVrGoodsNum(CacheClass.getIntValue(DataObj.vrgoodsid), userid);
		if(userVrgoodsNum==null || num>CacheClass.getFloatValue(userVrgoodsNum.num))
		{
			throw new LzPluginException("你的现货瓶数不足。");
		}
		
		//生成订单并申请发货
		boolean obj=CrowdPrestigeOrderProvider.getInstance().CreateVrGoodsOrder(userid,CacheClass.getIntValue(DataObj.vrgoodsid),num,DataObj.address);
		if(!obj)
		{
			throw new LzPluginException("申请发货失败");
		}
	
		// 生成result包给用户
		PublicData data = new PublicData();
		data.state = "1";
		data.m = m + "";
		data.info = "申请发货成功";
		
		return data;
	}	
	
	
}
